package ft.esc.impl;

import java.io.File;

import javafx.application.Application;
import javafx.application.Platform;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;

public class EchoSocketClientController
{
	public TextField txtRemoteHost;
	public TextField txtRemotePort;
	public Button btnCmdSelPath;
	public TextField txtSelPath;
	public Button btnCmdSend;
	public TextArea txtMain;
	
	/**
	 * 待发送文件选择对话框
	 */
	private FileChooser fcSelPath;
	
	StringBuffer _sb = new StringBuffer();
	
	/**
	 * Default Sel Path <br />路径选择默认值
	 */
	public static final String DEFAULT_SEL_PATH = "E:/temp";
	
	public static final String DEFAULT_REMOTE_PORT = "10001";
	
	public void initialize()
	{
		// 设置默认IP地址
		{
			String defHostIp = getCurrHostIp();
			if(defHostIp!=null && defHostIp.length()>0 && txtRemoteHost!=null)
			{
				txtRemoteHost.setText(defHostIp);
			}
		}
		
		// 设置默认远程端口
		txtRemotePort.setText(DEFAULT_REMOTE_PORT);
		
		// 设置默认发送文件所在路径
		txtSelPath.setText(DEFAULT_SEL_PATH);
		
		// 选择文件
		btnCmdSelPath.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				showFcSelPathDiag();
			}
		});
		
		EchoSocketClientController thisCtx = this;
		// 发送文件
		btnCmdSend.setOnAction(new EventHandler<ActionEvent>(){
			public void handle(ActionEvent event)
			{
				String strRemoteHost = getRemoteHostIp();
				String strRemotePort = getRemotePort();
				String strSelPath = getSelPath();
				
				if(strRemoteHost!=null && strRemoteHost.length()>0 &&
						strRemotePort!=null && strRemotePort.length()>0 &&
						strSelPath!=null && strSelPath.length()>0)
				{
					// 创建新线程
					FileSendClientImpl r = new FileSendClientImpl(strRemoteHost, strRemotePort, strSelPath, null);
					r.setCtx(thisCtx);
					Thread t = new Thread(r);
					t.setPriority(Thread.NORM_PRIORITY - 1);
					t.start();
					
					// 使按钮失效
					hideEditControls();
				}
			}
		});
	}
	
	/**
	 * get Sel Path<br />取得所选目录路径
	 */
	private String getSelPath()
	{
		String ret = "";
		if(txtSelPath!=null)
		{
			ret = txtSelPath.getText();
		}
		return ret;
	}
	
	private String getCurrHostIp()
	{
		String ret = "192.168.9.101";
		//
		return ret;
	}
	
	private String getRemoteHostIp()
	{
		String ret = "";
		if(txtRemoteHost!=null)
		{
			ret = txtRemoteHost.getText();
		}
		return ret;
	}
	
	private String getRemotePort()
	{
		String ret = "";
		if(txtRemotePort!=null)
		{
			ret = txtRemotePort.getText();
		}
		return ret;
	}
	
	public void postStr(String str)
	{
		if(str!=null && str.length()>0)
		{
			StringBuffer sb = getSb();
			if(sb!=null)
			{
				sb.append(str);
				sb.append('\n');
				
				txtMain.setText(sb.toString());
			}
		}
	}
	
	private StringBuffer getSb()
	{
		if(_sb!=null)
		{}
		else
		{
			_sb = new StringBuffer();
		}
		return _sb;
	}
	
	private void clearSb()
	{
		_sb = new StringBuffer();
	}
	
	/**
	 * 打开文件选择对话框
	 */
	private void showFcSelPathDiag()
	{
		File f = null;
		
		FileChooser fc = getFcSelPath();
		
		try
		{
			f = fc.showOpenDialog(null);
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
			ex.printStackTrace();
		}
		
		if(f!=null)
		{
			String fcPath = f.getPath();
//			System.out.println("f yes!");
//			System.out.println(f.getPath());
			postStr("对话框选择文件：" + fcPath);
			txtSelPath.setText(fcPath);
		}
		else
		{
//			System.out.println("f null");
			postStr("对话框被取消");
		}
	}
	
	private FileChooser getFcSelPath()
	{
		if(fcSelPath==null)
		{
			fcSelPath = new FileChooser();
			fcSelPath.setTitle("选择想要发送的文件");
		}
		return fcSelPath;
	}
	
	public void hideEditControls()
	{
		txtRemoteHost.setEditable(false);
		txtRemotePort.setEditable(false);
		txtSelPath.setEditable(false);
		btnCmdSelPath.setDisable(true);
		btnCmdSend.setDisable(true);
	}
	
	public void showEditControls()
	{
		txtRemoteHost.setEditable(true);
		txtRemotePort.setEditable(true);
		txtSelPath.setEditable(true);
		btnCmdSelPath.setDisable(false);
		btnCmdSend.setDisable(false);
	}
}
